/**
 * 1.本代码为云笔记web端开发小组共同拥有。
 * 2.组内成员有 聂道鑫、徐斌强、何瑞、钟倩。
 * 3.联系方式：442342707@qq.com
 */
package com.yunbiji.model;

import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.yunbiji.util.GetTime;

@SuppressWarnings("serial")
/**
 * 笔记模型类
 * @author 聂道鑫
 *
 */
public class Notes extends Model<Notes>
{
	public static final Notes dao = new Notes();

	private String getNotesSql = "from notes where tel =? and c_folder =?";
	
	private String updateNoteName_SQL="update notes set c_name=? where tel=? and id=?";
	
	private String INSERTCONTENT_SQL = "update notes set c_content=? where id=? and tel=?";

	
	/**
	 * 获取分页的列表内容
	 * @param tel
	 * @param idString
	 * @param pageno
	 * @return list
	 */
	public List<Notes> getNotes(String tel, String idString,int pageno)
	{
		int id=Integer.valueOf(idString);
		Page<Notes> page = Notes.dao.paginate(pageno, 9, " select * ",getNotesSql,tel,id);
		List<Notes> list = page.getList();
		return list;
	}
	/**
	 * 重命名笔记
	 * @param tel
	 * @param id
	 * @param noteName
	 * @return
	 */
	public boolean renameNote(String tel,int id,String noteName)
	{
		int i=Db.update(updateNoteName_SQL, noteName,tel,id);
		if(i==1)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	/**
	 * 更新笔记内容
	 * @param content
	 * @param id
	 * @param tel
	 * @return
	 */
	public boolean getContent(String content,int id,String tel)
	{
		int i= Db.update(INSERTCONTENT_SQL,content,id,tel);
		if(i==1)
		{
			return true;	
		}
		else
		{
			return false;
		}
	}
	
	/**
	 * 查询笔记的内容
	 * @author niedaoxin
	 * @param id
	 * @param tel
	 * @return String
	 */
	public String queryStrContent(int id,String tel)
	{
		return (Db.queryStr("SELECT c_content From notes WHERE id=? and tel=?",id, tel));
	}
	
	/**
	 * 得到笔记的名字
	 * @author niedaoxin
	 * @param id
	 * @param tel
	 * @return String
	 */
	public String queryStrName(int id,String tel)
	{
		return (Db.queryStr("select c_name from notes where tel=? and id=?",tel,id));
	}
	/**
	 * 获取当前笔记所在笔记本的id
	 * @param id
	 * @param tel
	 * @return int
	 */
	public int queryStrC_folder(int id,String tel)
	{
		return (Db.queryInt("SELECT c_folder From notes WHERE id=? and tel=?",id,tel));
	}
	
	/**
	 * 新增笔记
	 * @param tel
	 * @param noteName
	 * @param pId
	 */
	public void updateNotes(String tel,String noteName,String pId )
	{
		Db.update("insert into notes (tel,c_name,c_folder,c_foundtime) values(?,?,?,?)", tel,noteName,Integer.valueOf(pId),GetTime.dao.getTime());
	}
	/**
	 * 将刚刚新增的笔记的id取出
	 * @param tel
	 * @return
	 */
	public int queryIntId(String tel)
	{
		return (Db.queryInt("select max(id) from notes where tel=?",tel));
	}
	/**
	 * 根据id删除notes表中的笔记
	 * @param id
	 */
	public void deleteById(int id)
	{
		Db.deleteById("notes", id);
	}
}
